package com.kzd.btgame.main.mine

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.btgame.R
import com.kzd.btgame.custom.setTransitionStatusE
import com.kzd.btgame.databinding.ActivityMineTransferBinding
import com.kzd.btgame.entity.GameBean
import com.kzd.btgame.jetpack.vm.MineViewModel
import com.kzd.btgame.main.game.adapter.Game1Adapter
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel

class MineTransferActivity : OwnerViewBindingActivity<ActivityMineTransferBinding>() {

    private val mGame1Adapter = Game1Adapter<GameBean>(Game1Adapter.Type.GAME_LIST_TRANSFER)

    private val mViewModel: MineViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityMineTransferBinding
        get() = ActivityMineTransferBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flMineTransferBar
            barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        mViewBinding.includeAppRefresh.rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
        }.adapter = mGame1Adapter

        mViewBinding.includeAppRefresh.mSmartRefreshLayout.setOnRefreshListener {
            mViewModel.mineTransferList(isLoadMore = false)
        }
        mViewBinding.includeAppRefresh.mSmartRefreshLayout.setOnLoadMoreListener {
            mViewModel.mineTransferList(isLoadMore = true)
        }
    }

    override fun initData() {
        mViewModel.mineTransferList()
    }

    override fun initViewMode() {
        mViewModel.transferListLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusE(response,
                mGame1Adapter,
                mViewBinding.includeAppRefresh.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mGame1Adapter.addData(data)
            } else {
                mGame1Adapter.setHeaderView(createViewImage())
                mGame1Adapter.setNewInstance(data)
            }
        })
    }

    private fun createViewImage(): View = ImageView(this as Context).apply {
        layoutParams = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.WRAP_CONTENT
        )
        scaleType = ImageView.ScaleType.FIT_XY
        setImageResource(R.mipmap.ic_transfer_game_top)
    }
}